package kz.kstu.scantest.desktop.util.shell;

import kz.kstu.scantest.desktop.util.GUIUtils;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

/**
 * @author Filipp_Stankevich
 *         created: 09/29/11 4:26 PM
 */
public class StartScreen {

    public static final Logger LOGGER = Logger.getLogger(StartScreen.class);

    private static StartScreen instance;
    private static final String SCREEN_IMAGE_PATH = "/screen.jpg";

    private JFrame window;

    private StartScreen() {
    }

    public static StartScreen getInstance() {
        if (instance == null) {
            instance = new StartScreen();
        }
        return instance;
    }

    public void show() {
        window = new JFrame();
        window.setUndecorated(true);
        window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        URL imageUrl = this.getClass().getResource(SCREEN_IMAGE_PATH);
        try {
            BufferedImage image = ImageIO.read(imageUrl);
            ImageIcon imageIcon = new ImageIcon(image);
            JLabel imageLabel = new JLabel(imageIcon);
            window.add(imageLabel);
            window.setSize(image.getWidth(), image.getHeight());
        } catch (IOException e) {
            LOGGER.error("Can not read start screen image", e );
        }
        GUIUtils.centerAndShow(window);
    }

    public void hide() {
        window.dispose();
    }
}
